package lax.task.com;

import com.sun.net.httpserver.HttpServer;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.util.*;
import java.util.concurrent.Executors;

public class Main {
    public static void main(String[] args) throws IOException {
        String filePth = "config.properties";
        File file = new File(filePth);
        if (file.exists()) {
            // 文件存在
            Properties properties = new Properties();
            InputStream inputStream = new FileInputStream("config.properties");
            try {
                properties.load(inputStream);
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            AppData.taskRunTime = Integer.parseInt(properties.get("taskRunTime").toString());
            AppData.port = Integer.parseInt(properties.get("port").toString());
            AppData.intevalPeriod = Integer.parseInt(properties.get("intevalPeriod").toString());
        }
        AppData.TaskList = new LinkedList<>();
        InetSocketAddress addr = new InetSocketAddress(AppData.port);
        HttpServer server = HttpServer.create(addr, 0);
        server.createContext("/", new MyHandler());
        server.setExecutor(Executors.newCachedThreadPool());
        server.start();
        System.out.println("Server is listening on port " + AppData.port);
//        for(int i=1;i<10000;i++){
//            new TaskRun().AddTask(5, "http://www.tp.com","lax"+i);
//        }
//        new TaskRun().AddTask(5, "http://www.tp.com",);
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                // task to run goes here
                // 执行的输出的内容
                if (AppData.TaskList.size() > 0) {
                    TaskData taskData = AppData.TaskList.get(0);
                    long now_time = new Date().getTime() / 1000;
                    System.out.println(now_time);
                    long task_time = taskData.getTime();
                    int n_e = (int) (task_time - now_time);
                    if (n_e < 10) {
                        if (n_e > 0) {
                            String url = taskData.getUrl();
                            String name = taskData.getName();
                            System.out.println("TaskName:" + name);
                            new TaskRun().AddTask(n_e, url, name);
                        }
                        AppData.TaskList.remove(0);
                    }
                }
//                System.out.println("Main, stranger");
            }
        };
        Timer timer = new Timer();
        // 定义开始等待时间  --- 等待 1 秒
        // 1000ms = 1s
        long delay = AppData.taskRunTime;
        // 定义每次执行的间隔时间
        long intevalPeriod = AppData.intevalPeriod;
        // schedules the task to be run in an interval
        // 安排任务在一段时间内运行
        timer.scheduleAtFixedRate(task, delay, intevalPeriod);
    }
}